{include file='_header'}
<div class="page-header">
    当前位置：<span class="text-primary">{if !empty($item['id'])}编辑{else}添加{/if}商品 <small>{if !empty($item['id'])}修改【{$item['title']}】{/if}
</div>
<div class="page-content">
    <div class="page-sub-toolbar">
        <span class=''>
            <a class="btn btn-primary btn-sm" href="{:url('admin/groups/goodsadd')}">添加新商品</a>
        </span>
    </div>
{if !$category}
    <p style="height:30px;line-height: 30px;">暂无商品分类，请先添加商品分类</p>
    <a class="btn btn-primary btn-sm" href="{:url('admin/groups/categoryadd')}">添加新商品分类</a>
{else}
<form id="dataform" action="" method="post" class="form-horizontal form-validate">
    <input type='hidden' id='tab' name='tab' value='basic' />
    <input type='hidden' id='id' name='id' value="{$item['id']}" />
    <input type='hidden' id='gid' name='gid' value="{$item['gid']}" />
    <ul class="nav nav-arrow-next nav-tabs" id="myTab">
        <li {if $_GET['tab']=='basic' || empty($_GET['tab'])}class="active"{/if} ><a href="#tab_basic">商品</a></li>
        <li {if $_GET['tab']=='stock'}class="active"{/if} ><a href="#tab_stock">库存及编码</a></li>
        <li {if $_GET['tab']=='info'}class="active"{/if} ><a href="#tab_info">详情</a></li>
        <li {if $_GET['tab']=='marketing'}class="active"{/if} ><a href="#tab_marketing">营销</a></li>
        <li {if $_GET['tab']=='verify'}class="active"{/if}><a href="#tab_verify">线下核销</a></li>
    </ul>
    <div class="tab-content">
        <div class="tab-pane  {if $_GET['tab']=='basic' || empty($_GET['tab'])}active{/if}" id="tab_basic">{include file='groups/goods/basic'}</div>
        <div class="tab-pane  {if $_GET['tab']=='stock'}active{/if}" id="tab_stock">{include file='groups/goods/stock'}</div>
        <div class="tab-pane  {if $_GET['tab']=='info'}active{/if}" id="tab_info">{include file='groups/goods/info'}</div>
        <div class="tab-pane  {if $_GET['tab']=='marketing'}active{/if}" id="tab_marketing">{include file='groups/goods/marketing'}</div>
        <div class="tab-pane  {if $_GET['tab']=='verify'}active{/if}" id="tab_verify">{include file='groups/goods/verify'}</div>
    </div>
    <div class="form-group"></div>
    <div class="form-group">
        <label class="col-lg control-label"></label>
        <div class="col-sm-9 col-xs-12">
            <input type="submit"  value="保存商品" class="btn btn-primary" />
            <a class="btn btn-default " href="{:url('admin/groups/goods')}">返回列表</a>
        </div>
    </div>
</form>
</div>
<script language='javascript'>
    goods_id = "{$item['id']}";
    option_open = true;
    //是否开启了多规格
    more_spec = "{$item['more_spec']}";
    group_goods_id = "{$group_goods_id}";
    shop_goods_id = "{$item[gid]}";
    require(['bootstrap'],function(){
        $('#myTab a').click(function (e) {
            e.preventDefault();
            $('#tab').val( $(this).attr('href'));
            $(this).tab('show');
        })
    });
    $(function(){
        $('#more').click(function(){
            option_open = $('#more').is(':checked');
            if( option_open ){
                get_spec();
            }else{
                $( '#more_spec' ).hide();
            }
        });
    });
    function get_spec()
    {
        if( more_spec != 1 ) {
            $.getJSON("{:url('admin/groups/get_spec')}", {
                goods_id: goods_id,
                group_goods_id: group_goods_id,
                shop_goods_id:shop_goods_id
            }, function (msg) {
                if (msg.status == 1) {
                    $('#spec_s').empty();
                    $.each(msg.result.data, function (k, v) {
                        $('#spec_s').append("<tr><input type='hidden' name='spec[" + k + "][specs]' value='" + v.specs + "'><input type='hidden' name='spec[" + k + "][id]' value=''><input type='hidden' name='spec[" + k + "][goods_option_id]' value='" + v.id + "'>\n" +

                            "                    <td>\n" +

                            "                        <input type=\"text\" class=\"form-control   valid \" name='spec[" + k + "][name]' value='" + v.title + "' aria-invalid=\"false\">\n" +

                            "                    </td>\n" +

                            "                    <td>\n" +

                            "                        <input type=\"text\" class=\"form-control   valid option_marketprice\" name='spec[" + k + "][marketprice]' value='" + v.marketprice + "' aria-invalid=\"false\">\n" +

                            "                    </td>" +

                            "                   <td style='display:none;'>" +

                            "                       <input type=\"text\" class=\"form-control  valid option_single_price\" name=\"spec[" + k + "]['single_price']\" value='" + v.marketprice + "'>\n" +

                            "                   </td>" +

                            "                    <td class=\"type-4\">\n" +

                            "                        <input type=\"text\" class=\"form-control  option_price\" name='spec[" + k + "][price]' value=\"\">\n" +

                            "                    </td>\n" +

                            "                    <td class=\"type-4\">\n" +

                            "                        <input type=\"text\" class=\"form-control  option_stock\" name='spec[" + k + "][stock]' value='" + v.stock + "'>\n" +

                            "                    </td>\n" +

                            "                </tr>");

                    });
                } else {
                    //如果保存取消按钮开启状态
                    $('#more').attr('checked', false).next('div').removeClass('checked');
                    tip.msgbox.err(msg.result.message);
                    $( '#more_spec' ).hide();
                    return false;
                }
            });
        }
        $( '#more_spec' ).show();
    }
    $('form').submit(function(){
        if($(':radio[name=goodstype]:checked').val()=='0'){
            if($(':input[name=goodsid_text]').val()==''){
                return false;
            }
        }
        if($('select[name=cate]').val()==''){
            $('#myTab a[href="#tab_basic"]').tab('show');
            return false;
        }
        return true;
    });

    function toimgsrc(src) {
        if(typeof src != 'string') {
            return '';
        }
        if(src.indexOf('http://')==0 || src.indexOf('https://')==0 || src.indexOf('../addons')==0) {
            return src;
        }
        else if(src.indexOf('images/') == 0) {
            return "{$_W['attachurl']}" +  src;
        }
    }
    function select_goods(o){

        goods_id = o.gid;

        var $thumb = '';

        /*商品缩略图*/

        if(o.thumb){

            $thumb += '<div class="multi-item"><img src="'+ o.thumb +'" ' +

                    'onerror="this.src=\'/public/static/images/nopic.jpg\'; this.title=\'图片未找到.\'" class="img-responsive img-thumbnail">' +

                    '<input type="hidden" name="thumbs[0]" value="'+ o.thumb+'">' +

                    '<em class="close" title="删除这张图片" onclick="deleteMultiImage(this)">×<\/em></div>';

        }

        /*商品幻灯片*/

        if(o.thumb_url){

            $.each(o.thumb_url,function(index,value){

                var i = index++;

                $thumb += '<div class="multi-item"><img src="'+ value +'" ' +

                        'onerror="this.src=\'/public/static/images/nopic.jpg\'; this.title=\'图片未找到.\'" class="img-responsive img-thumbnail">' +

                        '<input type="hidden" name="thumbs['+i+']" value="'+value+'">' +

                        '<em class="close" title="删除这张图片" onclick="deleteMultiImage(this)">×<\/em></div>';

            });

        }

        $(".multi-img-details").html($thumb);

        $(":input[name=gid]").val(o.gid);

        $(":input[name=total]").val(o.total);

        $(":input[name=description]").val(o.subtitle);

        $(":input[name=price]").val(o.marketprice);

        $(":input[name=goodssn]").val(o.goodssn);

        $(":input[name=productsn]").val(o.productsn);

        var ue = UE.getEditor('content', {

            autoHeight: false

        });

        ue.ready(function() {
            //设置编辑器的内容
            ue.setContent(o.content);
        });
    }

    function setCol( class_ ){
        var value = $( '.'+class_+'_all' ).val();
        $.each( $( '.'+class_ ) , function(k,v){
            $( v ).val( value );
        });
    }
</script>
{/if}
{include file='_footer'}